package com.yzc.mihuyou.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yzc.mihuyou.entity.PageWuer;
import com.yzc.mihuyou.entity.User;
import com.yzc.mihuyou.mapper.UserMapper;
import com.yzc.mihuyou.service.AttentionService;
import com.yzc.mihuyou.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzc.mihuyou.service.UserimgService;
import com.yzc.mihuyou.vo.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 易紫诚
 * @since 2022-04-28
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    UserimgService userimgService;
    @Autowired
    AttentionService attentionService;

    @Override
    public User LoginUser(String name, String password) {
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper
                .eq("user_account",name)
                .eq("user_password",password);
        User user=userMapper.selectOne(wrapper);
       if(user==null){
           return null;
       }
        user.setUserNum(userMapper.getUserNum(user.getUserId()).getUserNum());
        user.setUserAtt(attentionService.allAttention(user.getUserId()));
        user.setAtt(attentionService.allFans(user.getUserId()));
        return user;
    }

    @Override
    public JsonResult addUser(User user) {
        HashMap<String,Object> map=new HashMap<>();
        map.put("user_account",user.getUserAccount());
        List<User> lists=userMapper.selectByMap(map);
        if(lists.size()>0){
            return  new JsonResult(401,"当前账号已存在");
        }
        int i=userMapper.insert(user);
        if(user.getUserNikname()==null){
            QueryWrapper<User> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("user_id",user.getUserId());
            userMapper.update(new User().setUserNikname("用户_"+user.getUserId()),queryWrapper);
        }
        if(i>0){
            return new JsonResult(200,"注册成功");
        }
        return new JsonResult(500,"出现异常");
    }

    @Override
    public Map<String, Object> selUser(Long id) {
       User user=userMapper.selectById(id);
       String imgUrl=userimgService.selImage(user.getUserPortrait());
       Map<String,Object> map =new HashMap<>();
       map.put("User",user);
       map.put("imgUrl",imgUrl);
        return map;
    }

    @Override
    public User cuw(Long uid) {
       User user=userMapper.selectById(uid);
       if(user==null){
           return  null;
       }
       user.setUserImage(userimgService.selImage(user.getUserPortrait()));
       System.out.println(user);
        return user;
    }

    @Override
    public JsonResult upUser(User user) {
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("user_id",user.getUserId());
        int i = userMapper.update(user,wrapper);
        if(i>0){
            Map<String,Object> map=new HashMap<>();
            map.put("user",this.cuw(user.getUserId()));
            return new JsonResult(200,"修改成功",map);
        }
        return new JsonResult(500,"出现异常");
    }


    @Override
    public JsonResult getUser(long id) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.select("user_id","user_account","user_portrait","user_individuality","gmt_create","user_nikname","user_gender");
        wrapper.eq("user_id",id);
        Map map = new HashMap();
        User user = userMapper.selectOne(wrapper);
        user.setUserNum(this.getUserNum(id));
        user.setUserImage(userimgService.selImage(user.getUserPortrait()));
        user.setUserAtt(attentionService.allAttention(id));
        user.setAtt(attentionService.allFans(id));
        map.put("data",user);
        if(map.size()>0){
            return new JsonResult(200,"查询成功",map);
        }
        return new JsonResult(500,"出现异常",map);
    }

    @Override
    public JsonResult likeUser(String str,PageWuer pageWuer) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        Map<String,Object> map = new HashMap<>();
        wrapper.select("user_id","user_account","user_portrait","gmt_create","user_individuality","user_nikname","user_gender");
        wrapper.like("user_nikname",str);
        System.out.println(str);
        Page<User> page = new Page<>(new Long(pageWuer.getCurrent()), new Long(pageWuer.getSize()));
        List<User> users = userMapper.selectPage(page,wrapper).getRecords();
        System.out.println(users);
        for(User user : users){
            user.setUserImage(userimgService.selImage(user.getUserId().intValue()));
        }

        map.put("data",users);
        if(map.size()>0){
            return new JsonResult(200,"查询成功",map);
        }
        return new JsonResult(500,"出现异常",map);
    }

    @Override
    public long getUserNum(long id) {
        System.out.println(userMapper.getUserNum(id).getUserNum()+"az");
        return userMapper.getUserNum(id).getUserNum();
    }


}
